//-----------------------------------------------------------------------
// <copyright file="BannerDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class BannerDAOBase
    {
        #region Common methods
        public virtual Banner CreateBannerFromReader(IDataReader reader)
        {
            Banner item = new Banner();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("BannerId"))) item.BannerId = (long)reader["BannerId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("BannePath"))) item.BannePath = (string)reader["BannePath"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Note"))) item.Note = (string)reader["Note"];
                if (!reader.IsDBNull(reader.GetOrdinal("BannerLink"))) item.BannerLink = (string)reader["BannerLink"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) item.StartDate = (DateTime)reader["StartDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDate"))) item.EndDate = (DateTime)reader["EndDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("BannerLocationId"))) item.BannerLocationId = (long)reader["BannerLocationId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (long)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("Clicked"))) item.Clicked = (long)reader["Clicked"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDefault"))) item.IsDefault = (bool)reader["IsDefault"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("FlashStartus"))) item.FlashStartus = (bool)reader["FlashStartus"];
                if (!reader.IsDBNull(reader.GetOrdinal("Object_TGKM"))) item.Object_TGKM = (long)reader["Object_TGKM"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateBanner methods
            
        public virtual void CreateBanner(Banner banner)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, banner.Title);
                database.AddInParameter(dbCommand, "@BannePath", DbType.String, banner.BannePath);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, banner.CreatedDate);
                database.AddInParameter(dbCommand, "@Note", DbType.String, banner.Note);
                database.AddInParameter(dbCommand, "@BannerLink", DbType.String, banner.BannerLink);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, banner.UpdatedDate);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, banner.StatusId);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, banner.MemberId);
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, banner.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, banner.EndDate);
                database.AddInParameter(dbCommand, "@BannerLocationId", DbType.Int64, banner.BannerLocationId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, banner.Viewed);
                database.AddInParameter(dbCommand, "@Clicked", DbType.Int64, banner.Clicked);
                database.AddInParameter(dbCommand, "@IsDefault", DbType.Boolean, banner.IsDefault);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, banner.Enable);
                database.AddInParameter(dbCommand, "@FlashStartus", DbType.Boolean, banner.FlashStartus);
                database.AddInParameter(dbCommand, "@Object_TGKM", DbType.Int64, banner.Object_TGKM);
                database.AddOutParameter(dbCommand, "@BannerId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                banner.BannerId = (long)database.GetParameterValue(dbCommand, "@BannerId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBannerException, ex);
            }
        }

        #endregion

        #region UpdateBanner methods
        
        public virtual void UpdateBanner(Banner banner)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerUpdate");            
                
                database.AddInParameter(dbCommand, "@BannerId", DbType.Int64, banner.BannerId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, banner.Title);
                database.AddInParameter(dbCommand, "@BannePath", DbType.String, banner.BannePath);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, banner.CreatedDate);
                database.AddInParameter(dbCommand, "@Note", DbType.String, banner.Note);
                database.AddInParameter(dbCommand, "@BannerLink", DbType.String, banner.BannerLink);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, banner.UpdatedDate);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, banner.StatusId);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, banner.MemberId);
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, banner.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, banner.EndDate);
                database.AddInParameter(dbCommand, "@BannerLocationId", DbType.Int64, banner.BannerLocationId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, banner.Viewed);
                database.AddInParameter(dbCommand, "@Clicked", DbType.Int64, banner.Clicked);
                database.AddInParameter(dbCommand, "@IsDefault", DbType.Boolean, banner.IsDefault);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, banner.Enable);
                database.AddInParameter(dbCommand, "@FlashStartus", DbType.Boolean, banner.FlashStartus);
                database.AddInParameter(dbCommand, "@Object_TGKM", DbType.Int64, banner.Object_TGKM);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBannerException, ex);
            }
        }
        
        #endregion

        #region DeleteBanner methods
        public virtual void DeleteBanner(long bannerId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerDelete");
                
                database.AddInParameter(dbCommand, "@BannerId", DbType.Int64, bannerId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBannerException, ex);
            }
        }

        #endregion

        #region GetBanner methods
        
        public virtual Banner GetBanner(long bannerId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerGet");
                
                database.AddInParameter(dbCommand, "@BannerId", DbType.Int64, bannerId);
                
                Banner banner =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        banner = CreateBannerFromReader(reader);
                        reader.Close();
                    }
                }
                return banner;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerException, ex);
            }
        }
        
        #endregion

        #region GetBannerList methods
        public virtual BannerCollection GetBannerList(BannerColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerCollection bannerCollection = new BannerCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Banner banner = CreateBannerFromReader(reader);
                        bannerCollection.Add(banner);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerListException, ex);
            }
        }
        
        public virtual BannerCollection GetBannerList(BannerColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBannerList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetBannerList methods
        public virtual BannerCollection GetBannerListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spBannerGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                BannerCollection bannerCollection = new BannerCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Banner banner = CreateBannerFromReader(reader);
                        bannerCollection.Add(banner);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return bannerCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBannerListException, ex);
            }
        }
        
        public virtual BannerCollection GetBannerListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetBannerListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}